package com.jsnkj.basic.bean

import com.jsnkj.basic.util.SharePreferencesUtil
import java.util.Arrays


/**
 * 请求异常回调信息
 */

class ErrorEntity {
    companion object {
        fun getInstance(code: Int, message: String?): ErrorEntity {
            val entity = ErrorEntity()
            entity.code = code
            entity.message = message

            if (message.isNullOrEmpty()) {
                return entity
            }
            try {
                val splitArray = message.split("\\|#\\|".toRegex()).dropLastWhile { it.isEmpty() }
                    .toTypedArray()

                val arrayList = ArrayList(Arrays.asList(*splitArray))

                for (str in arrayList) {
                    com.jsnkj.basic.util.LogUtils.e("分割数据：${arrayList.size} == " + str)
                }

                val settingEntity = SharePreferencesUtil.getInstance().getSettingEntity()
                if (null == settingEntity.invites) {
                    settingEntity.invites = InvitesBean()
                }

                if (arrayList.size > 1) {
                    settingEntity.app_version = arrayList[0]
                    settingEntity.invites!!.download_app_url = arrayList[1]
                    SharePreferencesUtil.getInstance().putSettingEntity(settingEntity)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            com.jsnkj.basic.util.LogUtils.e("数据：$entity")
            return entity
        }
    }

    var code: Int = -1
    var message: String? = ""


    override fun toString(): String {
        return "ErrorEntity(code=$code, message='$message')"
    }
}